#include "forget.h"
#include "ui_forget.h"
#include <QDebug>

Forget::Forget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Forget){
    ui->setupUi(this);

    connect(ui->btnQuit, &QPushButton::clicked, this, [=](){
        emit Login(INDEX_LOGIN);
    });
    connect(ui->btnForget, &QPushButton::clicked, this, &Forget::forget);
}

Forget::~Forget(){
    delete ui;
}

void Forget::forget(){
    UserDatabase user;
    QString name = ui->username->text();
    QString phone = ui->phone->text();
    QString password = ui->password->text();
    if(ui->username->text().isEmpty() || ui->phone->text().isEmpty() || ui->password->text().isEmpty()){
        return;
    }
    if(!user.openDatabase("users.db")){
        return;
    }
    if(user.verifyUserIdentity(name,phone)){
        if(user.updateUserPassword(name,password)){
            qDebug() << "修改成功";
            ui->username->clear();
            ui->phone->clear();
            ui->password->clear();
            user.closeDatabase();
            return;
        }
    }
    user.closeDatabase();
}
